package com.example.podcaster;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.example.podcaster.database.DBApplication;
import com.example.podcaster.databaseInterfaces.IPodcastTable;
import com.example.podcaster.util.PodcastListAdapter;

public class FavoritesAcitity extends SherlockActivity
{
    /**
     * URL zu einem Podcast-RSS-Feed mit Audioinhalten
     */

    // public static final String RSS_AUDIO =
    // "http://feeds.feedburner.com/mtvgameone";

    private ListView listView;

    private Cursor podcasts;

    private int[] finalcastids;
    private String [] finalnames;
    private String [] finalvalues;
    

    private String dbSortString = "ASC";

    private String[] sort;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_podcast_list);

        createListView();

        ActionBar actionBar = this.getSupportActionBar();
        actionBar.setTitle("");
        actionBar.setIcon(R.drawable.logo);

        listView.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
            {
                podcasts.moveToPosition(position);
                int id = finalcastids[position];
                int is_video = podcasts.getInt(3);
                Intent intent = new Intent(FavoritesAcitity.this, EpisodeListActivity.class);
                String title = finalnames[position];
                intent.putExtra("title", title);
                intent.putExtra("id", id);
                
                intent.putExtra("video", is_video);
                FavoritesAcitity.this.startActivity(intent);
            }

        });

        sort = getResources().getStringArray(R.array.sort_array);

        /** Create an array adapter to populate dropdownlist */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.sherlock_spinner_item, sort);

        /** Enabling dropdown list navigation for the action bar */
        getSupportActionBar().setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);

        /** Defining Navigation listener */
        ActionBar.OnNavigationListener navigationListener = new OnNavigationListener()
        {
            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId)
            {
                if (itemId == 0)
                {
                    dbSortString = "ASC";
                    createListView();
                    //Toast.makeText(getBaseContext(), "Sortierung nach : " + sort[itemPosition], Toast.LENGTH_SHORT).show();
                }
                if (itemId == 1)
                {

                    dbSortString = "DESC";
                    createListView();
                    //Toast.makeText(getBaseContext(), "Sortierung nach : " + sort[itemPosition], Toast.LENGTH_SHORT).show();
                }

                return false;

            }
        };

        /**
         * Setting dropdown items and item navigation listener for the actionbar
         */
        getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);
        adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    }

    protected void onRestart()
    {
        super.onRestart();
        createListView();
    }

    public void createListView()
    {
        if (dbSortString == "ASC")
        {
            podcasts = DBApplication.getSqliteDatabase().rawQuery(IPodcastTable.selectAll_id_url_titleASC, null);
        }

        if (dbSortString == "DESC")
        {
            podcasts = DBApplication.getSqliteDatabase().rawQuery(IPodcastTable.selectAll_id_url_titleDESC, null);
        }

        listView = (ListView) this.findViewById(R.id.podcastList);
        String[] values = new String[podcasts.getCount()];
        String[] names = new String[podcasts.getCount()];
        int[] ids = new int[podcasts.getCount()];
        int j = 0;
        while (podcasts.moveToNext())
        {
            if (getSharedPreferences("prefs", 0).contains(String.valueOf(podcasts.getInt(0))))
            {
                ids[j] = podcasts.getInt(0);
                names[j] = podcasts.getString(2);
                if (podcasts.getInt(3) == 0)
                {
                    values[j] = "audio";
                }
                else
                {
                    values[j] = "video";
                }
                j++;
            }

        }
        finalvalues = new String[j];
        finalnames = new String[j];
        finalcastids = new int[j];
        for (int i = 0; i < j; i++)
        {
            finalcastids[i] = ids[i];
            finalnames[i] = names[i];
            finalvalues[i] = values[i];
        }
        listView.setAdapter(new PodcastListAdapter(FavoritesAcitity.this, finalvalues, finalnames, finalcastids, getSharedPreferences("prefs", 0)));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        // Abfangen der ActionBar Items
        switch (item.getItemId())
        {
            case R.id.menu_search:
                onSearchRequested();
                return true;
            case R.id.menu_player:
                Intent intent1 = new Intent(FavoritesAcitity.this, PlayEpisodeActivity.class);
                intent1.putExtra("episodeId", EpisodeListActivity.getSharedID());
                FavoritesAcitity.this.startActivity(intent1);
                return true;    
            case R.id.menu_home:
                Intent intent = new Intent(FavoritesAcitity.this, HomeActivity.class);
                FavoritesAcitity.this.startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onSearchRequested()
    {
        Bundle appData = new Bundle();
        appData.putString("SearchView", "PodcastList");
        startSearch(null, false, appData, false);
        return true;

    }

    @Override
    protected void onDestroy()
    {
        podcasts.close();

        super.onDestroy();
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        if(PlayEpisodeActivity.getVisited()==true)
        {
            getSupportMenuInflater().inflate(R.menu.actionbar, menu);
        }
        else
        {
            getSupportMenuInflater().inflate(R.menu.actionbar_short, menu); 
        }

        return super.onCreateOptionsMenu(menu);
    }
}